package com.brillilab.service.logic.plans;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import com.brillilab.common.alioss.OssFileEnum;
import com.brillilab.common.entity.PageVo;
import com.brillilab.domain.enums.comments.CommentsClassifyEnum;
import com.brillilab.domain.enums.comments.CommentsContentTypeEnum;
import com.brillilab.domain.enums.file.FileTypeEnum;
import com.brillilab.domain.po.plans.PlanComment;
import com.brillilab.domain.po.system.FileManage;
import com.brillilab.domain.vo.comments.CommentsAddVo;
import com.brillilab.domain.vo.lab.MemberInfoVo;
import com.brillilab.domain.vo.plans.PlanCommentAddVo;
import com.brillilab.domain.vo.plans.PlanCommentVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.lab.ILabMemberService;
import com.brillilab.service.core.plans.IPlanCommentService;
import com.brillilab.service.core.system.IFileManageService;
import com.brillilab.service.core.user.IUsersService;
import com.brillilab.service.logic.comments.CommentsLogic;
import com.brillilab.service.logic.user.UsersLogic;

/**
 * @author zlb
 * @Title: PlanCommentLogic
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2019/9/11 13:46
 */
@Service
@Transactional
public class PlanCommentLogic {
	@Resource
	private IPlanCommentService planCommentService;
	@Resource
	private IUsersService usersService;
	@Resource
	private ILabMemberService labMemberService;
	@Resource
	private UsersLogic usersLogic;
	@Resource
	private IFileManageService fileManageService;
	@Resource
	private CommentsLogic commentsLogic;

	/**
	 * 根据参数获取实验计划评论列表
	 *
	 * @param userInfo
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Deprecated
	public List<PlanCommentVo> getPlanCommentList(Long planMemberId, String startDate, String endDate) {

		List<PlanCommentVo> planCommentVoList = new ArrayList<>();
		List<PlanComment> planCommentList = planCommentService.getPlanCommentList(planMemberId, startDate, endDate);

		if (!CollectionUtils.isEmpty(planCommentList)) {
			// 评论人ID集合
			List<Long> labMemberIdList = planCommentList.stream().map(PlanComment::getLabMemberId).distinct()
					.collect(Collectors.toList());
			List<MemberInfoVo> memberList = labMemberService.selectMemberVoListByIds(labMemberIdList);
			Map<Long, List<MemberInfoVo>> memberMap = memberList.stream()
					.collect(Collectors.groupingBy(MemberInfoVo::getLabMemberId));
			for (PlanComment planComment : planCommentList) {
				PlanCommentVo planCommentVo = new PlanCommentVo();
				BeanUtils.copyProperties(planComment, planCommentVo);
				List<MemberInfoVo> list = memberMap.get(planComment.getLabMemberId());
				if (!CollectionUtils.isEmpty(list)) {
					planCommentVo.setName(list.get(0).getUserName());
					planCommentVo.setUrl(list.get(0).getImageUrl());
				}

				// 评论文件
				List<FileManage> fileManageList = fileManageService.selectList(OssFileEnum.PlanCommentFile,
						planComment.getId());
				if (!CollectionUtils.isEmpty(fileManageList)) {
					List<FileManage> imgFileList = fileManageList.stream()
							.filter(p -> p.getFileType().equals(FileTypeEnum.Image.getValue()))
							.collect(Collectors.toList());
					planCommentVo.setImgFileList(imgFileList);
					List<FileManage> videoFileList = fileManageList.stream()
							.filter(p -> p.getFileType().equals(FileTypeEnum.Video.getValue()))
							.collect(Collectors.toList());
					planCommentVo.setVideoFileList(videoFileList);
					List<FileManage> audioFileList = fileManageList.stream()
							.filter(p -> p.getFileType().equals(FileTypeEnum.Audio.getValue()))
							.collect(Collectors.toList());
					planCommentVo.setAudioFileList(audioFileList);
				}

				planCommentVoList.add(planCommentVo);
			}
		}

		return planCommentVoList;
	}

	/**
	 * 添加计划评论
	 * 
	 * @param addVo
	 * @param userInfo
	 * @return
	 */
	public Long addPlanComment(PlanCommentAddVo addVo, UserInfoVo userInfo) {
		Assert.notNull(addVo, "参数有误！");
		PlanComment planComment = new PlanComment();
		BeanUtils.copyProperties(addVo, planComment);
		planComment.setLabMemberId(userInfo.getLabMemberId());
		PlanComment insert = planCommentService.insert(planComment);
		// 处理附件
		if (insert != null && ArrayUtils.isNotEmpty(addVo.getCommentFileIds())) {
			for (Long fileId : addVo.getCommentFileIds()) {
				FileManage fileManage = fileManageService.selectById(fileId);
				fileManage.setReferDataId(insert.getId());
				fileManage.setAttachType(OssFileEnum.PlanCommentFile.getKey());
				fileManageService.updateById(fileManage);
			}
		}
		return insert != null ? insert.getId() : 0;
	}

	/**
	 * 删除计划评论
	 * 
	 * @param commentId
	 * @return
	 */
	public Boolean deletePlanComment(Long commentId) {
		return planCommentService.deleteById(commentId);
	}

	/**
	 * 获取评论分页
	 * 
	 * @param planMemberId
	 * @param pageNum
	 * @param pageSize
	 * @param sort   排序：0-降序（默认），1-升序
	 * @return
	 */
	public PageVo<PlanCommentVo> commentPage(Long planMemberId, Integer pageNum, Integer pageSize) {
		PageVo<PlanComment> page = planCommentService.commentPage(planMemberId, pageNum, pageSize);
		List<PlanCommentVo> planCommentVoList = new ArrayList<>();
		if (!CollectionUtils.isEmpty(page.getContent())) {
			// 评论人ID集合
			List<Long> labMemberIdList = page.getContent().stream().map(PlanComment::getLabMemberId).distinct()
					.collect(Collectors.toList());
			List<MemberInfoVo> memberList = labMemberService.selectMemberVoListByIds(labMemberIdList);
			Map<Long, List<MemberInfoVo>> memberMap = memberList.stream()
					.collect(Collectors.groupingBy(MemberInfoVo::getLabMemberId));
			for (PlanComment planComment : page.getContent()) {
				PlanCommentVo planCommentVo = new PlanCommentVo();
				BeanUtils.copyProperties(planComment, planCommentVo);
				List<MemberInfoVo> list = memberMap.get(planComment.getLabMemberId());
				if (!CollectionUtils.isEmpty(list)) {
					planCommentVo.setName(list.get(0).getUserName());
					planCommentVo.setUrl(list.get(0).getImageUrl());
				}

				// 评论文件
				List<FileManage> fileManageList = fileManageService.selectList(OssFileEnum.PlanCommentFile,
						planComment.getId());
				if (!CollectionUtils.isEmpty(fileManageList)) {
					List<FileManage> imgFileList = fileManageList.stream()
							.filter(p -> p.getFileType().equals(FileTypeEnum.Image.getValue()))
							.collect(Collectors.toList());
					planCommentVo.setImgFileList(imgFileList);
					List<FileManage> videoFileList = fileManageList.stream()
							.filter(p -> p.getFileType().equals(FileTypeEnum.Video.getValue()))
							.collect(Collectors.toList());
					planCommentVo.setVideoFileList(videoFileList);
					List<FileManage> audioFileList = fileManageList.stream()
							.filter(p -> p.getFileType().equals(FileTypeEnum.Audio.getValue()))
							.collect(Collectors.toList());
					planCommentVo.setAudioFileList(audioFileList);
				}
				planCommentVoList.add(planCommentVo);
			}
		}

		return new PageVo<PlanCommentVo>(page.getPageNum(), page.getPageSize(), page.getTotalRow(), planCommentVoList);
	}
}
